import React, { createContext, ReactElement } from 'react';
import { reduce } from 'ramda';

import { State, Action, Context } from './types';
import actions, { getReload } from './actions';
import Provider from '../../components/Provider';
// import { getPayMethod, setPayMethod } from '../../store/pay';
import { PayMethod, isIOS } from '../../constant';
// import { getInitCouponInfo } from '../../util/coupon';

export const initialState: State = {
  couponList: [],
  courseCommodityInfo: {
    hasFreight: false,
    hasAddress: false,
    receiverAddress: {
      province: '',
      city: '',
      district: '',
      detail: '',
      receiverName: '',
      receiverMobilePhone: ''
    },
    skus: [],
    unionDiscountAmount: 0,
    coupons: [],
    productId: 0,
    skuIds: [],
    totalCoinAmount: 0,
    needSignProtocol: false,
    protocolLink: ''
  },
  payMethod: PayMethod.Unknown,
  isFetchFailed: false,
  orderId: '',
  selectedCouponList: [],
  isFirstVisit: false,
  validCouponNumber: 0,
  agreementSelected: false,
  yutouCoinNum: 0,
  spuList: []
};

export const reducer = (state: State, action: Action): State => {
  switch (action.type) {
    case 'SET_COURSE_COMMODITY': {
      // const { isFirstVisit, selectedCouponList } = state;
      // const { coupons, productId, skus, unionDiscountAmount } = action.value;
      // const skuAmount = reduce(
      //   (a, b): number => a + b,
      //   0,
      //   skus.map((item): number => item.price)
      // );

      // const payAmount = skuAmount - unionDiscountAmount;

      // const {
      //   selectedCouponList: initSelectedCouponList,
      //   validCouponNumber
      // } = getInitCouponInfo(productId, payAmount, coupons);

      // let payMethod = 1; // getPayMethod();
      // if (isIOS) {
      //   payMethod = PayMethod.YutouCoin;
      // }

      return {
        ...state
        // isFirstVisit: true,
        // courseCommodityInfo: action.value,
        // couponList: coupons
        // selectedCouponList:
        //   initSelectedCouponList && !isFirstVisit
        //     ? initSelectedCouponList
        //     : selectedCouponList,
        // isFetchFailed: false,
        // payMethod,
        // validCouponNumber
      };
    }
    case 'FETCH_FAILED':
      return { ...state, isFetchFailed: true };
    case 'SET_PAY_METHOD': {
      // setPayMethod(action.value);
      return {
        ...state,
        payMethod: action.value
      };
    }
    case 'SET_ORDER_ID': {
      return {
        ...state,
        orderId: action.value
      };
    }
    case 'SET_SELECTED_COUPON_LIST': {
      return {
        ...state,
        selectedCouponList: action.value
      };
    }
    case 'SET_AGREEMENT_SELECTED': {
      return {
        ...state,
        agreementSelected: action.value
      };
    }
    case 'SET_YUTOU_COIN_NUM': {
      return {
        ...state,
        yutouCoinNum: action.value
      };
    }
    case 'SET_SPU_LIST': {
      return {
        ...state,
        spuList: action.value
      };
    }
    default:
      return state;
  }
};

export const Store = createContext<Context>({
  state: initialState
});

export default (props: { [key: string]: any }): ReactElement => {
  return (
    <Provider
      reducer={reducer}
      initialState={initialState}
      Store={Store}
      actions={actions}
      reload={getReload}
      {...props}
    />
  );
};
